package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleService vehicleService;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务:参数={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //为每一个围栏VO补全数据
        for (int i = 0; i < list.size(); i++) {
            GeofenceVO geofenceVO = list.get(i);
            //封装车辆查询条件
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏上绑定的车辆总数
            int totalNum = vehicleVOList.size();
            //定义变量保存围栏上的可用车辆数
            int availableNum = 0;
            //遍历车辆列表,获取每个车辆是否可用,如果出现可用车辆,availableNum++
            for(VehicleVO vehicleVO : vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //给当前GeofenceVO补全数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存电子围栏业务:参数={}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam, geofence);
        if (geofence.getId() == null) {//新增
            geofence.setStatus("1");//设置围栏的初始状态为启用
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        } else {//更新
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务:{}", id);
        /* 在删除围栏前,需要查此围栏上是否有绑定车辆,若有,提示用户无法删除! */
        //1.封装车辆的查询条件:当前围栏id
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆模块业务层的方法
        List<VehicleVO> list = vehicleService.selectVehicle(vehicleQuery);
        //3.判断:如果该围栏下有车,业务层抛出异常!
        if (list.size() > 0 && list != null) {
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        } else {
            //4.如果围栏下没有车辆,正常删除围栏
            geofenceMapper.deleteById(id);
        }
    }
}
